package top.hkyzf.study.leetcode.dp;

import org.junit.Test;

/**
 * @author 朱峰
 * @date 2022-1-30 17:24
 */
public class 打家劫舍_198 {
    public int rob1(int[] nums) {
        return dp(nums, nums.length);
    }

    int dp(int[] nums, int k) {
        if (k == 1) {
            return nums[0];
        }
        if (k == 2) {
            return Math.max(nums[0], nums[1]);
        }
        return Math.max(dp(nums, k-2) + nums[k-1], dp(nums, k-1));
    }

    public int rob(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int k = nums.length;
        if (k == 1) {
            return nums[0];
        }
        int[] dp = new int[k];
        dp[0] = nums[0];
        dp[1] = Math.max(nums[0], nums[1]);
        for (int i = 2; i < k; i++) {
            dp[i] = Math.max(dp[i-2] + nums[i], dp[i-1]);
        }
        return dp[k-1];
    }

    @Test
    public void testRob () {
        int[] nums = {2,7,9,3,1};
        System.out.println(rob(nums));
    }
}
